package com.housekeeping.housekeeping.cleanerInformation.controller;

import com.alibaba.fastjson.JSON;

import com.housekeeping.housekeeping.cleanerInformation.entity.Cleaner;
import com.housekeeping.housekeeping.cleanerInformation.service.ICleanerService;
import com.housekeeping.housekeeping.cleanerInformation.service.impl.CleanerServiceImpl;
import com.housekeeping.housekeeping.cleanerInformation.utils.ResponseEntity;
import com.housekeeping.housekeeping.cleanerInformation.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author hua
 * @version JDK1.8
 * @date 2021/1/24 15:48
 *      根据id删除选中的所有数据
 */
@WebServlet("/cleanerInformation/controller/CleanerDeleteAllController")
public class CleanerDeleteAllController extends HttpServlet {
    private ICleanerService cleanerService = new CleanerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求字符集
        request.setCharacterEncoding("utf-8");
        //设置响应字符集
        response.setContentType("text/html;charset=utf-8");

        String message = request.getParameter("message");
        System.out.println(message);
        List<Cleaner> list = JSON.parseArray(message,Cleaner.class);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        ResponseEntity<Object> entity = null;
        try {
            int i = cleanerService.cleanerDeleteAll(list);
            if (i > 0){
                entity = ResponseEntity.success(null);
            }else{
                entity = ResponseEntity.error(405,"删除失败");
            }
        } catch (SQLException throwables) {
            entity = ResponseEntity.error(402,"网络异常,请稍后再试");
        }
        ResponseUtils.resp(response,entity);
    }
}
